<template>
    <div id="app">
        <router-view v-if="isRouterAlive"></router-view>
        <div v-if="tools" class="tools">
            <img @click="tools = false,toolsPassword =''" style="position:absolute;right:10px;top:10px;cursor:pointer;" src="../content/assets/img/home/close.png" alt="">
            <input class="toolsPassword" v-model="toolsPassword" @keyup.enter="openDevTools" autofocus="autofocus" type="password" placeholder="输入暗号" />
            <button class="openTools" @click="openDevTools">打开调试窗口</button>
        </div>
    </div>
</template>

<script>
import { SqlLib } from "../../../static/lib/sqlite/SqlLib";
import { Machine } from "../../../static/lib/sqlite/model/Machine";
import { Ini } from './utils/ini';

export default {
    name: 'my-project',
    provide () {
        return {
            reload: this.reload
        }
    },
    data () {
        return {
            isRouterAlive: true,
            tools:false,
            toolsPassword:'',
        }
    },
    mounted () {
        window.$log.info("程序开始启动...")
        window.document.onselectstart = () => false
        window.document.ondragstart = () => false
        window.document.oncontextmenu = () => false
        //注册全局key事件
        this.regesterKeyEvent();
        window.eduLevel = Ini.readIni('data', 'eduLevel')
        //全局存储数据目录
        this.initGlobalDir();
        window.document.addEventListener('mousemove', this.checkSuportTouch)
        //插入系统信息
        // this.insertPlatformInfo();

    },
    methods: {
        checkSuportTouch (e) {
            console.log(e)
            window.isTouchScreen = true
            window.document.removeEventListener('mousemove', this.checkSuportTouch)
        },
        regesterKeyEvent () {
            let that = this;
            window.onkeydown = function (event) {
                if (event.altKey && event.shiftKey && event.key == 'F12') {
                    that.tools = true
                    // that.$electron.remote.getCurrentWindow().webContents.openDevTools();
                    // console.log(that.$electron.remote.app.getAppPath());
                }
            }
        },
        openDevTools(){
            let that = this;
            if (this.toolsPassword === 'xzsmbos') {
                that.toolsPassword = ''
                that.tools = false
                that.$electron.remote.getCurrentWindow().webContents.openDevTools();
            }
        },
        initGlobalDir () {
            const path = require('path');
            let that = this;
            //全局存储数据目录
            let dataDir = this.$readIniFile('data', 'dataDir');
            console.log("now dataDir --> " + dataDir);
            if (dataDir === "self") {//代表是当前目录
                if (process.env.NODE_ENV === 'production') {
                    window.$DATADIR = path.dirname(process.execPath) + '/resources/data/';
                } else {
                    window.$DATADIR = './data/';
                }
            } else if (dataDir === "low-update") {
                window.$DATADIR = path.dirname(process.execPath) + '/html/';
            } else {
                window.$DATADIR = dataDir;
            }
            //初始化dbPath
            let dbPath = ""
            if (process.env.NODE_ENV === 'production') {
                dbPath = window.$DATADIR + that.$CONST.global.db;
            } else {
                dbPath = window.$DATADIR + that.$CONST.global.dbTest;
            }
            window.$DBDIR = dbPath;
        },
        insertPlatformInfo () {
            const os = require("os");
            let machine = new Machine();
            machine.id = 1;
            machine.hostName = os.hostname();
            machine.type = os.type();
            machine.platform = JSON.stringify(os.platform());
            machine.arch = os.arch();
            machine.totalmem = os.totalmem() + "";
            machine.freemem = os.freemem() + "";
            machine.cpus = JSON.stringify(os.cpus());
            SqlLib.getInstance(window.$DBDIR).updateOrInsertObjectById(machine);
        },
        reload () {
            this.isRouterAlive = false
            this.$nextTick(function () {
                this.isRouterAlive = true
            })
        },

    }
}
</script>

<style lang="scss">
/* CSS */
#app {
    width: 100vw;
    height: 100vh;
    border-radius: 8px;
    overflow: hidden;
}
.tools{
    background: linear-gradient(#ccf0ff, #8fc8e0);
    width: 500px;
    height: 300px;
    position: absolute;
    top: calc(50vh - 150px);
    left:calc(50vw - 250px);
    z-index: 10000!important;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border-radius: 8px;
    .toolsPassword{
        border-radius: 4px;
        width: 300px;
        height: 60px !important;
        font-size: 20px;
        background: rgb(236, 240, 240);
    }
    .openTools{
        margin-top: 60px;
        width:200px;
        height:60px;
        border-radius:4px;
        border: none;
        color:#fff;
        font-size: 20px;
        background-color: #63b4d6;
        cursor: pointer;
    }
} 
</style>
